package cc.yugu.yhdw.webserver.service.basic;

import cc.yugu.yhdw.common.domain.basic.CustomerForegift;
import cc.yugu.yhdw.common.entity.json.DataResult;
import cc.yugu.yhdw.common.entity.json.ExtResult;
import cc.yugu.yhdw.common.entity.pagination.Page;
import cc.yugu.yhdw.webserver.persistence.basic.CustomerForegiftMapper;
import cc.yugu.yhdw.webserver.persistence.hdw.AgencyMapper;
import cc.yugu.yhdw.webserver.service.AbstractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 电池押金业务
 *
 * @Date:Created in 2018/8/2 11:42
 * @Author:
 * @Modified By: zxj 2018/8/2 11:42
 */
@Service
public class CustomerForegiftService extends AbstractService {

    @Autowired
    CustomerForegiftMapper customerForegiftMapper;
    @Autowired
    AgencyMapper agencyMapper;
    /**
     * 表格翻页
     * @Param [search]
     * @Return cc.yugu.yhdw.common.entity.pagination.Page
     * @Author zxj 2018/8/2
     */
    public Page findPage(CustomerForegift search) {
        /*
         查询客户押金设置
         1.代理商
                --如果没有设置，查询运营商的设置
                --如果有设置，显示自己的设置
         2.运营商
                --必须设置，自己的设置的代理商id属性为null
        */
        Page page = search.buildPage();
        List<CustomerForegift> list = null;
        int count = customerForegiftMapper.findPageCount(search);
        // 代理商 -- 这个业务逻辑要重新设计
        if (search.getAgencyId() != null && count <= 0) {
            // 代理商没有客户押金设置，自动使用上级设置的客户押金设置
            // 查询这个代理商的上级运营商
            search.setAgentId(agencyMapper.findInfo(search.getAgencyId()).getAgentId());
            search.setAgencyId(null);
            page.setTotalItems(customerForegiftMapper.findPageCounts(search));
            search.setBeginIndex(page.getOffset());
            list = customerForegiftMapper.findPageResults(search);
        } else {
            page.setTotalItems(customerForegiftMapper.findPageCount(search));
            search.setBeginIndex(page.getOffset());
            list = customerForegiftMapper.findPageResult(search);
        }
        // 其中包括了代理商名称和运营商名称（使用左连接实现的）
        page.setResult(list);
        return page;
    }
    /**
     * 根据id查询客户押金
     * @Param [id]
     * @Return cc.yugu.yhdw.common.domain.basic.CustomerForegift
     * @Author zxj 2018/8/2
     */
    public CustomerForegift find(int id) {
        return customerForegiftMapper.find(id);
    }
    /**
     * 新建
     * @Param [entity]
     * @Return cc.yugu.yhdw.common.entity.json.DataResult
     * @Author zxj 2018/8/2
     */
    public DataResult create(CustomerForegift entity) {
        entity.setCreateTime(new Date());
        // 插入
        int total = customerForegiftMapper.insert(entity);
        if (total == 0) {
            return DataResult.failResult("操作失败！", "");
        }
        return DataResult.successResult(entity.getId());
    }
    /**
     * 修改（代理商没有修改的权限）
     * @Param [entity]
     * @Return cc.yugu.yhdw.common.entity.json.ExtResult
     * @Author zxj 2018/8/2
     */
    public ExtResult update(CustomerForegift entity) {
        entity.setUpdateTime(new Date());
        int total = customerForegiftMapper.update(entity);
        if (total == 0) {
            return ExtResult.failResult("操作失败！");
        }
        return ExtResult.successResult();
    }
    /**
     * 删除（根据id）
     * @Param [id]
     * @Return cc.yugu.yhdw.common.entity.json.ExtResult
     * @Author zxj 2018/8/2
     */
    public ExtResult delete(int id) {
        int total = customerForegiftMapper.delete(id);
        if (total == 0) {
            return ExtResult.failResult("操作失败！");
        }
        return ExtResult.successResult();
    }

}
